import json

from django.http import HttpResponse
from django.shortcuts import render

from wisdoms import deviceManager
from wisdoms.Memory.MemoryController import MemoryController
from wisdoms.processManagement import 进程管理
from django.template.context_processors import csrf
from wisdoms.File import FileManage


# # 初始化设备
# deviceManager.main()
# # 文件初始化
# FileManage.main()
# # 文件控制器初始化
# FileController = FileManage.PM(MemoryController)
# # 文件显示实例
# FileShow = FileManage.SC()
#
#
# # 内存控制器初始化
# MemoryController = MemoryController()


def index(request):
    return render(request, 'index.html')


uniteText = 进程管理.showUnite()


def showIntergrated(request):
    request.POST.get('value')
    ctx = {}
    ctx.update(csrf(request))
    dict = next(uniteText)
    print(dict)
    print(进程管理.fileShow())
    if len(dict) == 0:
        return render(request, 'intergrated.html', {'process': json.dumps(emptyDict, ensure_ascii=False),
                                                    'memory': json.dumps(进程管理.memoryShow(), ensure_ascii=False),
                                                    'file': json.dumps(进程管理.fileShow(), ensure_ascii=False),
                                                    'device': json.dumps(进程管理.deviceShow(), ensure_ascii=False)})
    # if request.POST.get('SP') == 1:
    else:
        return render(request, 'intergrated.html', {'process': json.dumps(dict, ensure_ascii=False),
                                                    'memory': json.dumps(进程管理.memoryShow(), ensure_ascii=False),
                                                    'file': json.dumps(进程管理.fileShow(), ensure_ascii=False),
                                                    'device': json.dumps(进程管理.deviceShow(), ensure_ascii=False)})


# 设备显示
def showDevice(request):
    request.POST.get('value')
    ctx = {}
    ctx.update(csrf(request))
    # 如果POST存在
    if request.POST['SD'] == '1':
        return render(request, 'device.html', {'device': json.dumps(进程管理.deviceShow(), ensure_ascii=False)})
        # return HttpResponse(json.dumps(deviceManager.deviceShow(), ensure_ascii=False))
    else:
        return HttpResponse('Error')


# 内存占用显示
def showMemory(request):
    request.POST.get('value')
    ctx = {}
    ctx.update(csrf(request))
    if request.POST['SM'] == 1:
        return render(request, 'memory.html', {'memory': json.dumps(进程管理.memoryShow(), ensure_ascii=False)})


# 分模块展示：文件
def showFile(request):
    return render(request, 'file.html')


# 创建文件
def createFile(request):
    request.POST.get('value')
    ctx = {}
    ctx.update(csrf(request))
    createLoc = request.POST.get('createLoc')
    createSize = int(request.POST.get('createSize'))
    FileManage._mk(createLoc, createSize)
    print(进程管理.fileShow())
    return render(request, 'file.html', {'memory': json.dumps(进程管理.memoryShow(), ensure_ascii=False),
                                         'file': json.dumps(进程管理.fileShow(), ensure_ascii=False)})


def deleteFile(request):
    request.POST.get('value')
    ctx = {}
    ctx.update(csrf(request))
    createLoc = request.POST.get('createLoc')
    createSize = request.POST.get('createSize')
    deleteFileLoc = request.POST.get('deleteFileLoc')
    deleteFolderLoc = request.POST.get('deleteFolderLoc')
    print(进程管理.fileShow())
    if len(dict) == 0:
        return render(request, 'file.html', {'memory': json.dumps(进程管理.memoryShow(), ensure_ascii=False),
                                             'file': json.dumps(进程管理.fileShow(), ensure_ascii=False)})
    # if request.POST.get('SP') == 1:
    else:
        return render(request, 'intergrated.html', {'process': json.dumps(dict, ensure_ascii=False),
                                                    'memory': json.dumps(进程管理.memoryShow(), ensure_ascii=False),
                                                    'file': json.dumps(进程管理.fileShow(), ensure_ascii=False),
                                                    'device': json.dumps(进程管理.deviceShow(), ensure_ascii=False)})


def deleteFolder(request):
    request.POST.get('value')
    ctx = {}
    ctx.update(csrf(request))
    createLoc = request.POST.get('createLoc')
    createSize = request.POST.get('createSize')
    deleteFileLoc = request.POST.get('deleteFileLoc')
    deleteFolderLoc = request.POST.get('deleteFolderLoc')
    print(进程管理.fileShow())
    if len(dict) == 0:
        return render(request, 'file.html', {'memory': json.dumps(进程管理.memoryShow(), ensure_ascii=False),
                                             'file': json.dumps(进程管理.fileShow(), ensure_ascii=False)})
    # if request.POST.get('SP') == 1:
    else:
        return render(request, 'intergrated.html', {'process': json.dumps(dict, ensure_ascii=False),
                                                    'memory': json.dumps(进程管理.memoryShow(), ensure_ascii=False),
                                                    'file': json.dumps(进程管理.fileShow(), ensure_ascii=False),
                                                    'device': json.dumps(进程管理.deviceShow(), ensure_ascii=False)})


# 文件管理不用加jsondumps

testDict = [{'index': 1, 'typeItem': 0, 'baseTime': 1, 'duration': 1},
            {'index': 2, 'typeItem': 2, 'baseTime': 4, 'duration': 2},
            {'index': 1, 'typeItem': 0, 'baseTime': 6, 'duration': 1},
            {'index': 1, 'typeItem': 0, 'baseTime': 7, 'duration': 1},
            {'index': 1, 'typeItem': 0, 'baseTime': 50, 'duration': 1}]
emptyDict = [{'index': 1, 'typeItem': 0, 'baseTime': 0, 'duration': 0}]
FIFStext = 进程管理.showFIFS()


def showProcess(request):
    request.POST.get('value')
    ctx = {}
    ctx.update(csrf(request))
    dict = next(FIFStext)
    print(dict)
    if len(dict) == 0:
        return render(request, 'process.html', {'process': json.dumps(emptyDict, ensure_ascii=False)})
    # if request.POST.get('SP') == 1:
    else:
        return render(request, 'process.html', {'process': json.dumps(dict, ensure_ascii=False)})
    #
    # print(testDict)
    # return render(request, 'process.html', {'process': json.dumps(testDict, ensure_ascii=False)})


# 设备添加
def deviceAdd(request):
    deviceManager.add_dct()


# 设备删除

def home(request):
    return render(request, 'interface.html')


# 主页界面
def admin(request):
    return render(request, 'index.html')
